/**
 *	@file	telseq_safari.c
 *	@brief	サファリ園長　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "field/syswork.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "safari/safari_sys.h"
#include "savedata/safari_sv.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_safari.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

/**
 *	@brief	電話　サファリ園長の会話抽選
 */
u16 PMTelPlayL_Safari(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	u16 mission;

	wk->seq_code = TEL_SEQ_SIMPLE;

	if(wk->ev_type == TEL_RECEIVE_EVENT){	//イベント着信
		//サファリオープン
		return TEL_TK_SAFARI_EV_OPEN;
	}

	//ノーマル着信(予約)
	if(wk->is_receive){
		SAFARI_SAVE* safari_sv = SaveData_GetSafariSave(man->save);

		switch(wk->ev_id){
		case 0:	//マップアップデート
			wk->seq_code = TEL_SEQ_SAFARI_RECEIVE_MAP;
			return 0;

		case TEL_TK_SAFARI_EV_M2_NOTICE:
			SysWork_SetSafariMissionStatus( man->ev_sv, SAFARI_M2_OK );
			return wk->ev_id;

		case TEL_TK_SAFARI_EV_OBJ_ADD_INI:
		case TEL_TK_SAFARI_EV_OBJ_ADD:
			SafariSv_ObjLevelUp(safari_sv,1);
			//次へのキータイム設定
			SafariSv_SetEventKeyTime(safari_sv,	SaveData_GetPlayTime(man->save));
			break;
		case TEL_TK_SAFARI_EV_OBJ_ADD_END:
		case TEL_TK_SAFARI_EV_OBJ_ADD_ALL:
			SafariSv_ObjLevelUp(safari_sv,SAFARI_OBJ_LEVEL_MAX);
			break;
		default:
			wk->ev_id = TEL_TK_SAFARI_CALL_LOOP;
			return wk->ev_id;	//セーフティ
		}
		//OBJ追加系
		SysWork_SetSafariMissionStatus( man->ev_sv, SAFARI_MISSION_END );
		return wk->ev_id;
	}
	//発信

	//同一マップにいるか？
	if(wk->pbk->map_id == man->zone_id)	{
		return TEL_TK_SAFARI_NE;
	}
	mission = SysWork_GetSafariMissionStatus(man->ev_sv);

	return (mission+TEL_TK_SAFARI_CALL_B_OPEN);
}

/**
 *	@brief	サファリ園長の着信会話シーケンス(マップのレベルアップ会話)
 */
int PMTelSeq_TypeSafariReceiveMap(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		{
			u8 num,i;
			u8* history = PMTelSv_SafariUpdateHistoryGetAlloc(man->tel_sv,&num,man->heapID);

			if(num == 0){//この条件では呼ばれないはずだが一応
				PMTel_MsgPrintGmmSex(man,man->m_person,m_safari26_m,m_safari26_f);
			}else if(num >= 6){
				PMTel_MsgPrintGmmSex(man,man->m_person,m_safari12_m,m_safari12_f);
			}else{
				for(i = 0;i < num;i++){
					WORDSET_RegisterSafariAreaName(man->wset,WORDSET_TEL_LOCAL_BUF+i,history[i]);
				}
				num = ((num-1)*2);
				PMTel_MsgPrintGmmSex(man,man->m_person,m_safari07_m+num,m_safari07_f+num);
			}
			sys_FreeMemoryEz(history);
		}
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}
